package cn.lbd.io;


import lombok.AllArgsConstructor;
import lombok.Data;
import org.junit.Before;
import org.junit.Test;

import java.io.*;

public class Demo1 {

    @Test
    public void test1() throws IOException {
        String fileName = "D:\\IO\\demo1.txt";
        FileInputStream inputStream = new FileInputStream(fileName);
        byte[] buffer = new byte[1024];
        int len = 0;
        //按照字节数组读取
        //表示从FileInputStream从文件中一次最多读取1024字节
        //实际读取到的个数  达到文件末尾而没有更多的数据，则返回-1
        while ((len = inputStream.read(buffer)) != -1) {
            System.out.println(new String(buffer, 0, len));
        }
        inputStream.close();
    }

    @Test
    public void test2() throws IOException {
        String fileName = "D:\\IO\\demo2.txt";
        FileOutputStream fileOutputStream = new FileOutputStream(fileName, true);
        byte[] bytes = "打耳洞.....".getBytes();
        fileOutputStream.write(bytes);
        System.out.println("文件创建成功。。。");
        fileOutputStream.close();
    }

    //图片从D盘拷贝到F盘
    @Test
    public void test3() {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("D:\\IO\\rinv.jpg");
            fos = new FileOutputStream("E:\\IO\\rinv.jpg", true);
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
            System.out.println("拷贝完成");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void test4() {
        String fileName = "D:\\IO\\demo1\\demo11";
        File file = new File(fileName);
        boolean mkdirs = file.mkdirs();
        System.out.println(mkdirs);

        String fileName1 = "hello.txt";
        File file1 = new File(file, fileName1);
        try {
            System.out.println(file1.createNewFile());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test5() {
        String direct = "E:\\mytemp";
        File file = new File(direct);
        if (!file.exists()) {
            if (file.mkdirs()) {
                System.out.println("创建目录成功");
            } else {
                System.out.println("创建目录失败");
            }
        }

        String fileName = "hello.txt";
        File file1 = new File(file, fileName);
        if (!file1.exists()) {
            try {
                if (file1.createNewFile()) {
                    System.out.println("创建文件成功");
                } else {
                    System.out.println("创建文件失败");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("该文件已经存在，不要重复创建");
        }

        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(direct + "\\" + fileName);
            fileOutputStream.write("hello,world!\r\nhello,java".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                System.out.println("写入内容成功");
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    @Test
    public void test6() throws IOException {
        String fileName = "D:\\IO\\demo1.txt";
        FileReader fileReader = new FileReader(fileName);
        char[] buffer = new char[1024];
        int len = 0;
        while ((len = fileReader.read(buffer)) != -1) {
            System.out.println(new String(buffer, 0, len));
        }
        fileReader.close();
    }

    @Test
    public void test7() throws IOException {
        String fileName = "D:\\IO\\demo1.txt";
        FileWriter fileWriter = new FileWriter(fileName, true);
        fileWriter.write("你好你好\r\n");
        fileWriter.close();
    }

    @Test
    public void test8() throws IOException {
        String fileName1 = "D:\\IO\\rinv.jpg";
        String fileName2 = "E:\\IO\\rinv.jpg";

        BufferedInputStream inputStream = new BufferedInputStream(
                new FileInputStream(fileName1));
        BufferedOutputStream outputStream = new BufferedOutputStream(
                new FileOutputStream(fileName2));
        int len = 0;
        byte[] buffer = new byte[1024];
        while ((len = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer,0,len);
        }
        System.out.println("拷贝成功");
        outputStream.close();
        inputStream.close();
    }

    @Test
    public void test9() throws IOException {
        String fileName = "D:\\IO\\demo1.txt";
        //装饰者模式
        BufferedReader reader = new BufferedReader(new FileReader(fileName));
        String str;
        int count = 1;
        while ((str = reader.readLine()) != null) {
            System.out.println("第"+(count++)+"行："+str);
        }
        if (reader != null) {
            reader.close();
        }
    }

    //拷贝
    @Test
    public void test10() throws IOException {
        String fileName1 = "D:\\IO\\demo1.txt";
        String fileName2 = "D:\\IO\\demo2.txt";
        //装饰者模式
        //创建缓冲里对象，套接在指定流基础上
        BufferedReader reader = new BufferedReader(new FileReader(fileName1));
        BufferedWriter writer = new BufferedWriter(new FileWriter(fileName2));
        //边读边写
        String str;
        int count = 1;
        //每次读一行
        while ((str = reader.readLine()) != null) {
            writer.write(new String("第"+(count++)+"行："+str));
            writer.newLine();//插入换行
        }
        System.out.println("ok~");
        if (writer != null) {
            writer.close();
        }
        if (reader != null) {
            reader.close();
        }
    }

    @Test
    public void test11() throws IOException {
        String fileName = "D:\\IO\\demo4.dat";
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(fileName));
        Dog dog = new Dog(1,"胡时狗");
        objectOutputStream.writeObject(dog);
        objectOutputStream.writeInt(100);
        objectOutputStream.close();
        System.out.println("序列化成功");
    }

    @Test
    public void test12() throws IOException, ClassNotFoundException {
        String fileName = "D:\\IO\\demo4.dat";
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(fileName));
        Dog dog = (Dog) objectInputStream.readObject();
        System.out.println(dog);
        int i = objectInputStream.readInt();
        System.out.println(i);
        System.out.println("反序列化成功");
    }

    @Test
    public void test13() throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\IO\\demo6.txt"),"gbk"));
        String line = "";
        while ((line = bufferedReader.readLine())!=null){
            System.out.println(line);
        }
        bufferedReader.close();
    }

    @Test
    public void test14() throws IOException{
        BufferedWriter bufferedWriter =new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\IO\\demo6.txt"),"gbk"));
        bufferedWriter.write("PHP是世界上最好的语言");
        bufferedWriter.close();
    }

}

@Data
@AllArgsConstructor
class Dog implements Serializable{
    private static final long serialVersionUID = 1L;
    int id;
    String name;
}
